package com.hgw.Demo06_Singleton.type4;

/**
 * Data time:2021/12/11 13:28
 * StudentID:2019112118
 * Author:hgw
 * Description: 懒汉式（线程安全 ）
 */
public class SingletonTest04 {

    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1==instance2);
    }

}

class Singleton {
    private static Singleton instance;

    private Singleton(){}

    // 提供一个静态的公有方法，加入同步处理的代码 synchronized，解决线程安全问题
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}